package wowraidmanager.client.wishlist;

import wowraidmanager.client.wishlist.about.About;
import wowraidmanager.client.wishlist.admin.Import;
import wowraidmanager.client.wishlist.admin.ImportlistGameVersion;
import wowraidmanager.client.wishlist.admin.Userwishlist;
import wowraidmanager.client.wishlist.contact.Contact;
import wowraidmanager.client.wishlist.news.News;
import wowraidmanager.client.wishlist.top.TopGameVersion;
import wowraidmanager.client.wishlist.user.CreateSubuser;
import wowraidmanager.client.wishlist.user.Createuser;
import wowraidmanager.client.wishlist.user.Restoreuserlist;
import wowraidmanager.client.wishlist.user.Userlist;
import wowraidmanager.client.wishlist.wishlist.EditwishlistGameVersion;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Widget;

public class Index extends Composite{

    private static IndexUiBinder uiBinder = GWT.create(IndexUiBinder.class);

    private UserStatus userStatus;

    interface IndexUiBinder extends UiBinder<Widget, Index>{
    }

    Member currentMember;

    @UiField
    MenuItem topMenu;

    @UiField
    MenuItem adminMenu;

    @UiField
    MenuItem adminImport;

    @UiField
    MenuItem adminImportlist;

    @UiField
    MenuItem adminUserwishlist;

    @UiField
    MenuItem userMenu;

    @UiField
    MenuItem userUserlist;

    @UiField
    MenuItem userCreateuser;

    @UiField
    MenuItem userCreatesubcharacter;

    @UiField
    MenuItem userRestoreuserlist;

    @UiField
    MenuItem wishlistMenu;

    @UiField
    MenuItem wishlistEditwishlist;

    @UiField
    MenuItem news;

    @UiField
    MenuItem contact;

    @UiField
    MenuItem about;

    @UiField
    MenuItem logoutMenu;

    @UiField
    FlowPanel mainPanel;

    public Index(UserStatus result){
        initWidget(uiBinder.createAndBindUi(this));
        userStatus = result;
        setMenuVisible();
        setMenuEvent();
        setTop();
    }

    private void setMenuEvent(){
        topMenu.setCommand(new Command(){

            public void execute(){
                setRemove();
                setTop();
            }
        });

        adminImport.setCommand(new Command(){

            public void execute(){
                setRemove();
                setAdminImport();
            }
        });

        adminImportlist.setCommand(new Command(){

            public void execute(){
                setRemove();
                setAdminImportlist();

            }
        });

        adminUserwishlist.setCommand(new Command(){

            public void execute(){
                setRemove();
                setAdminUserwishlist();
            }
        });

        userUserlist.setCommand(new Command(){

            public void execute(){
                setRemove();
                setUserUserlist();

            }
        });

        userCreateuser.setCommand(new Command(){

            public void execute(){
                setRemove();
                setUserCreateuser();

            }
        });

        userCreatesubcharacter.setCommand(new Command(){

            public void execute(){
                setRemove();
                setUserCreatesubcharacter();

            }
        });

        userRestoreuserlist.setCommand(new Command(){

            public void execute(){
                setRemove();
                setUserRestoreuserlist();
            }
        });

        wishlistEditwishlist.setCommand(new Command(){

            public void execute(){
                setRemove();
                setEditwishlist();
            }
        });

        news.setCommand(new Command(){

            public void execute(){
                setRemove();
                setNews();

            }
        });

        contact.setCommand(new Command(){

            public void execute(){
                setRemove();
                setContact();

            }
        });

        about.setCommand(new Command(){

            public void execute(){
                setRemove();
                setAbout();
            }
        });

        logoutMenu.setCommand(new Command(){

            public void execute(){
                setLogoutUrl();
            }
        });
    }

    protected void setUserCreatesubcharacter(){
        mainPanel.add(new CreateSubuser(userStatus));
    }

    protected void setUserRestoreuserlist(){
        mainPanel.add(new Restoreuserlist(userStatus));
    }

    protected void setNews(){
        mainPanel.add(new News(userStatus));
    }

    protected void setAbout(){
        mainPanel.add(new About());
    }

    protected void setContact(){
        mainPanel.add(new Contact(userStatus));
    }

    private void setTop(){
        mainPanel.add(new TopGameVersion(userStatus));
    }

    protected void setAdminImport(){
        mainPanel.add(new Import());
    }

    protected void setAdminImportlist(){
        mainPanel.add(new ImportlistGameVersion());
    }

    protected void setAdminUserwishlist(){
        mainPanel.add(new Userwishlist());
    }

    protected void setUserUserlist(){
        mainPanel.add(new Userlist(userStatus));
    }

    protected void setUserCreateuser(){
        mainPanel.add(new Createuser(userStatus));
    }

    protected void setEditwishlist(){
        mainPanel.add(new EditwishlistGameVersion(userStatus));
    }

    private void setLogoutUrl(){
        Window.open(userStatus.getLogoutUrl(), "_self", "");
    }

    protected void setRemove(){
        mainPanel.remove(0);
    }

    private void setMenuVisible(){
        if(!userStatus.isAdmin()){
            adminMenu.setVisible(false);
        }

        if(!userStatus.isOfficer() && !userStatus.isAdmin()){
            userCreateuser.setVisible(false);
            userRestoreuserlist.setVisible(false);
        }
    }

}
